import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:wallpaper/components/AlertDialog/my_alert_dialog.dart';
import 'package:wallpaper/components/appbar_base.dart';
import 'package:wallpaper/components/empty.dart';
import 'package:wallpaper/components/images/image_load.dart';
import 'package:wallpaper/components/images/image_view.dart';
import 'package:wallpaper/db/love_database.dart';
import 'package:wallpaper/model/options_base.dart';

class LovePage extends StatefulWidget {
  const LovePage({super.key});

  @override
  State<LovePage> createState() => _LovePageState();
}

class _LovePageState extends State<LovePage> {
  List imgs = [];

  final ScrollController _scrollController = ScrollController();
  void getData() async {
    var res = await context.read<LoveDatabase>().getAllLove();
    setState(() {
      imgs = res;
    });
  }

  @override
  void initState() {
    getData();
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppbarBase(title: '我的收藏'),
      floatingActionButton: SizedBox(
        height: 45,
        width: 45,
        child: FloatingActionButton(
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(50),
          ),
          child: Icon(Icons.delete, color: Colors.red),
          onPressed: () {
            MyAlertDialog(
              title: '删除',
              content: '确定删除所有收藏吗？',
              onPressed: () {
                context.read<LoveDatabase>().removeAllLove();
                setState(() {
                  imgs = [];
                });
              },
            ).show(context);
          },
        ),
      ),
      body: Padding(
        padding: const EdgeInsets.all(1),
        // ignore: prefer_is_empty
        child: imgs.length > 0
            ? GridView.builder(
                controller: _scrollController,
                gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                  crossAxisCount: OptionsBase().imageColumns(context),
                  childAspectRatio: 0.7,
                ),
                itemCount: imgs.length,
                itemBuilder: (context, index) {
                  return Container(
                    margin: EdgeInsets.all(OptionsBase().padding / 2),
                    decoration: BoxDecoration(
                        color: Theme.of(context).colorScheme.secondary,
                        borderRadius: BorderRadius.circular(10),
                        boxShadow: [
                          BoxShadow(
                            color: Theme.of(context).colorScheme.shadow,
                            offset: Offset(1, 2),
                            blurRadius: 2,
                          ),
                        ]),
                    child: ClipRRect(
                      borderRadius: BorderRadius.circular(5),
                      child: GestureDetector(
                        onTap: () {
                          Navigator.push(
                              context,
                              MaterialPageRoute(
                                  builder: (context) => ImageView(
                                        images: imgs,
                                        selectedIndex: index,
                                        resetGetdata: () {
                                          Future.delayed(
                                            Duration(milliseconds: 500),
                                            () => getData(),
                                          );
                                        },
                                      )));
                        },
                        child:
                            Stack(alignment: Alignment.bottomLeft, children: [
                          SizedBox(
                              width: double.infinity,
                              height: double.infinity,
                              child:
                                  ImageLoad(imageUrl: imgs[index].smallPath)),
                          Container(
                              color: const Color.fromARGB(24, 158, 158, 158),
                              width: double.infinity,
                              height: imgs[index].tags == "" ? 0 : 20,
                              child: Padding(
                                padding:
                                    const EdgeInsets.only(left: 5, right: 5),
                                child: Text(
                                  imgs[index].tags,
                                  overflow: TextOverflow.ellipsis,
                                  maxLines: 1,
                                  style: TextStyle(
                                      color: Colors.white, fontSize: 13),
                                ),
                              )),
                        ]),
                      ),
                    ),
                  );
                },
              )
            : Empty(),
      ),
    );
  }
}
